package com.jjtm.common.core.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author wangbaofeng
 *
 */
public class QrCodeUtil {

	/**
     * 生成二维码
	 * @param content 内容
	 * @param fileName 名称
	 * @param filePath 图片路径
	 * @param width	宽
	 * @param height 高
	 */
	public static void qrEncode(String content, String fileName, String filePath,Integer width,Integer height) {
		// 图像类型
		String format = "png";
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(16);
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		try {
			// 生成矩阵
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
			// 删除白边
			bitMatrix = deleteWhite(bitMatrix);
			Path path = FileSystems.getDefault().getPath(filePath, fileName);
			// 输出图像
			MatrixToImageWriter.writeToPath(bitMatrix, format, path);
		} catch (WriterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成二维码
	 * @param content 内容
	 * @param fileName 名称
	 * @param filePath 图片路径
	 * @param width	宽
	 * @param height 高
	 */
	public static MultipartFile qrEncode1(String content, String fileName, String filePath, Integer width, Integer height) {
		// 图像类型
		String format = "png";
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(16);
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		try {
			// 生成矩阵
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
			// 删除白边
			bitMatrix = deleteWhite(bitMatrix);
			Path file = new File("/img.png").toPath();
			MatrixToImageWriter.writeToPath(bitMatrix, format, file);
			FileItem fileItem = getMultipartFile(file.toFile(), "File");
			MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
			return multipartFile;

		} catch (WriterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
     * 删除白边
	 */
	private static BitMatrix deleteWhite(BitMatrix matrix) {
		int[] rec = matrix.getEnclosingRectangle();
		int resWidth = rec[2] + 1;
		int resHeight = rec[3] + 1;

		BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
		resMatrix.clear();
		for (int i = 0; i < resWidth; i++) {
			for (int j = 0; j < resHeight; j++) {
				if (matrix.get(i + rec[0], j + rec[1])) {
					resMatrix.set(i, j);
				}
			}
		}
		return resMatrix;
	}

	private static FileItem getMultipartFile(File file, String fieldName) {
		FileItemFactory factory = new DiskFileItemFactory(16, null);
		FileItem item = factory.createItem(fieldName, "text/plain", true, file.getName());
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		try {
			FileInputStream fis = new FileInputStream(file);
			OutputStream os = item.getOutputStream();
			while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return item;
	}
}
